﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Management;
using Atlandiz.Data;
using System.Windows.Forms;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    class AtlandizSmo
    {
        public static bool Shell()
        {
            //CHECKCERTIFICATE
            bool rt = false;
            string CertificateString = Army.ProgramBaseCode + '|' + Army.Serial + '|' + AtlandizSmo.GetSecureId() + '|' + Army.ExpDate + '|' + Army.LicenseClass + '|' + Army.isMovable.ToString();
            if (DateTime.Parse(Army.ExpDate) >= DateTime.Now)
            {
                if (Atlandiz.Data.Hash.VerifyHash(CertificateString, "SHA1", Army.Certificate))
                {
                    rt = true;
                }
                else
                {
                    //File.Delete(Application.StartupPath + "\\License.ini");
                }
            }
            rt = true; //Lisans Kontrol devre dışı..
            return rt;
        }

        public static bool ShellIndoor()
        {
            //CHECKCLimi@
            bool rt = true;
            if (AtlandizSmo.Shell() == false)
            {
                int thechk = Army.SQL().cmd("Update ZimmetHDetay SET extender='' where isDeleted=0");
                if (thechk >= 6)
                {
                    rt = false;
                }
            }
            return rt;
        }

        public static string GetProcessorID()
        {
            string sProcessorID = "";
            string sQuery = "SELECT ProcessorId FROM Win32_Processor";
            ManagementObjectSearcher oManagementObjectSearcher = new ManagementObjectSearcher(sQuery);
            ManagementObjectCollection oCollection = oManagementObjectSearcher.Get();
            foreach (ManagementObject oManagementObject in oCollection)
            {
                sProcessorID = (string)oManagementObject["ProcessorId"];
            }
            return (sProcessorID);
        }

        public static string GetSystemDrive()
        {
            string m_SystemDrive = "";
            string sQuery = "SELECT * FROM Win32_OperatingSystem";
            ManagementObjectSearcher oManagementObjectSearcher = new ManagementObjectSearcher(sQuery);
            ManagementObjectCollection oCollection = oManagementObjectSearcher.Get();
            foreach (ManagementObject oManagementObject in oCollection)
            {
                m_SystemDrive = (string)oManagementObject["SystemDrive"];
            }
            return (m_SystemDrive);
        }

        public static string GetSystemDriveSerial()
        {
            string m_SystemDriveSerial = "";
            string sQuery = "SELECT * FROM Win32_LogicalDisk where DriveType=3 and caption='" + GetSystemDrive() + "'";
            ManagementObjectSearcher oManagementObjectSearcher = new ManagementObjectSearcher(sQuery);
            ManagementObjectCollection oCollection = oManagementObjectSearcher.Get();
            foreach (ManagementObject oManagementObject in oCollection)
            {
                m_SystemDriveSerial = (string)oManagementObject["VolumeSerialNumber"];
            }
            return (m_SystemDriveSerial);
        }

        public static string GetSecureId()
        {
            string ProcessorID = AtlandizSmo.GetProcessorID();
            string DiskSerial = AtlandizSmo.GetSystemDriveSerial();
            string SecureId = ProcessorID.Substring(13) + ProcessorID.Substring(1, 4) + DiskSerial + ProcessorID.Substring(4, 4);
            SecureId = AtlandizSecurity.Encrypt(SecureId);
            return SecureId;
        }
    }

    class AtlandizSecurity
    {
        static readonly string PasswordHash = "^'+'@æs>£31";
        static readonly string SaltKey = "ASD^'4$#>3ßæasd@eq2345'+%'!'sadæß";
        static readonly string VIKey = "@+B225DAe5XsF6g7H8";

        public static string Encrypt(string plainText)
        {
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

            byte[] cipherTextBytes;

            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    cipherTextBytes = memoryStream.ToArray();
                    cryptoStream.Close();
                }
                memoryStream.Close();
            }
            return Convert.ToBase64String(cipherTextBytes);
        }

        public static string Decrypt(string encryptedText)
        {
            byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

            var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
            var memoryStream = new MemoryStream(cipherTextBytes);
            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
        }
    }
}
